﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;

namespace huaguwang
{
    public class D_T_NewsInfo_Exp : D_T_NewsInfo
    {

        /// <summary>
        /// 查找分页数据
        /// </summary>
        /// <param name="begin">起始记录数</param>
        /// <param name="pagesize">页大小</param>
        /// <param name="title">搜索关键字:标题</param>
        /// <param name="catetitle">搜索关键字:类别名称</param>
        /// <param name="count">总记录数</param>
        /// <returns>查询结果数据</returns>
        public List<huaguwang.M_T_NewsInfo_Exp.clsFind> GetList(int begin, int pagesize, string title, string catetitle, out int count)
        {
            var tab = " T_NewsInfo t1,T_NewsCate t2  ";
            var con = " t1.Flag = 1 and t2.Flag = 1 and t1.NewsCateID = t2.id";
            if (!string.IsNullOrEmpty(title))
            {
                con += " and t1.Title like '%'+@title+'%' ";
            }
            if (!string.IsNullOrEmpty(catetitle))
            {
                con += " and t2.CateName like '%'+@catetitle+'%' ";
            }

            SqlParameter[] parms = { 
                                   new SqlParameter("@title",SqlDbType.NVarChar,50),
                                   new SqlParameter("@catetitle",SqlDbType.NVarChar,50)
                                   };
            parms[0].Value = title.Exp_Trim();
            parms[1].Value = catetitle.Exp_Trim();

            var countSql = ExpandMSSQL.GetPageSQL(tab, con);
            count = MSSQLHelper.SQLConType.C1.Exp_GetSingle(
                countSql,
                parms
                ).Exp_IntTryParse();

            string strSQL = ExpandMSSQL.GetPageSQL(
                   begin,
                   pagesize,
                   "t1.id,t1.Title,t2.CateName,t1.Topis,t1.RemSign",
                   tab,
                   "t1.id",
                  con,
                   "t1.id desc"
                   );

            var List = MSSQLHelper.SQLConType.C1.Exp_GetList(
                strSQL,
                DR => new huaguwang.M_T_NewsInfo_Exp.clsFind()
                {
                    id = DR["id"].Exp_IntTryParse(),
                    Title = DR["Title"].Exp_Trim(),
                    CateTitle = DR["CateName"].Exp_Trim(),
                    Topis = DR["Topis"].Exp_IntTryParse() > 0 ? "置顶" : "未置顶",
                    RemSign = DR["RemSign"].Exp_BoolTryParse() ? "已推荐" : "未推荐"
                },
                parms
                );
            return List;
        }

        /// <summary>
        /// 检测名称是否重复
        /// </summary>
        /// <param name="title">新闻标题</param>
        /// <param name="petid">新闻类别ID</param>
        /// <param name="id">新闻id,大于0表示修改,小于等于0表示添加</param>
        /// <returns>true时表示重复</returns>
        public bool Exists(string title, int petid, int id)
        {
            StringBuilder strSQL = new StringBuilder();
            strSQL.Append("SELECT ID FROM T_NewsInfo WHERE Title=@title AND Flag = 1 and NewsCateID = @PetId ");
            strSQL.Append((id > 0) ? " and ID <> @ID " : "");
            SqlParameter[] parms = {
                                        new SqlParameter("@title",SqlDbType.NVarChar,50),
                                        new SqlParameter("@PetId",SqlDbType.Int,4),
                                        new SqlParameter("@ID",SqlDbType.Int,4)};
            parms[0].Value = title;
            parms[1].Value = petid;
            parms[2].Value = id;
            return MSSQLHelper.SQLConType.C1.Exp_Exists(strSQL.ToString(), parms);
        }

    }
}
